import httpClient from '@/lib/http-client';
import {cleanName} from '@/lib/utils';
import memoize from 'memoize';
import {db} from "@/lib/db";

// export const memorizedBkzjGn = memoize(async () => await fetchBkzjGn(), {maxAge: 3500});
// export const memorizedBkzjHy = memoize(async () => await fetchBkzjHy(), {maxAge: 3500});
export const memorizedETF = memoize(async (upOrDown) => await fetchETF(upOrDown), {maxAge: 5000});
export const memorizedETFDown = memoize(async (num) => await fetchETFDown(num), {maxAge: 5000});
export const memorizedEastMixDown = memoize(async () => await fetchEastMixDown(), {maxAge: 5000})
export const memorizedEastBKUp = memoize(async () => {
    const gns = await fetchBkGn();
    const hys = await fetchBkHy();
    return [...gns, ...hys]
}, {maxAge: 5000})
// // 板块概念资金
// async function fetchBkzjGn() {
//     const url =
//         'https://push2.eastmoney.com/api/qt/clist/get?fid=f62&po=1&pz=10&pn=1&np=1&fltt=2&invt=2&fs=m%3A90+t%3A3&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1%2Cf13';
//     const data = await httpClient.get(url);
//     return data.data.diff || [];
// }
//
// // 板块hy资金
// async function fetchBkzjHy() {
//     const url =
//         'https://push2.eastmoney.com/api/qt/clist/get?fid=f62&po=1&pz=10&pn=1&np=1&fltt=2&invt=2&fs=m%3A90+t%3A2&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1%2Cf13';
//     const data = await httpClient.get(url);
//     return data.data.diff || [];
// }

// 板块gn涨跌
async function fetchBkGn() {
    const url =
        'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m:90+t:3+f:!50&fields=f12,f13,f14,f1,f2,f4,f3,f152,f20,f8,f104,f105,f128,f140,f141,f207,f208,f209,f136,f222&fid=f3&pn=1&pz=20&po=1&dect=1&wbp2u=|0|0|0|web';
    const data = await httpClient.get(url);
    return data.data.diff || [];
}

// 板块hy涨跌
async function fetchBkHy() {
    const url =
        'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m:90+t:2+f:!50&fields=f12,f13,f14,f1,f2,f4,f3,f152,f20,f8,f104,f105,f128,f140,f141,f207,f208,f209,f136,f222&fid=f3&pn=1&pz=20&po=1&dect=1&wbp2u=|0|0|0|web';
    const data = await httpClient.get(url);
    return data.data.diff || [];
}

async function fetchETF(upOrDown: boolean) {
    const etfUrl =
        'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=e:16&fields=f12,f13,f14,f1,f2,f4,f3,f152,f5,f6,f17,f18,f15,f16&fid=f3&pn=1&pz=13&dect=1&wbp2u=%7C0%7C0%7C0%7Cweb';
    const url = upOrDown ? `${etfUrl}&po=1` : `${etfUrl}&po=0`;
    const resp = await httpClient.get(url);
    const values = resp.data?.diff || [];
    return values.map((v: { f14: string; f3: number }) => ({etf_name: cleanName(v.f14), chg: v.f3 / 100}));
}

export async function fetchHKStock() {
    const url =
        'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m:128+t:3,m:128+t:4,m:128+t:1,m:128+t:2&fields=f12,f13,f14,f19,f1,f2,f4,f3,f152,f17,f18,f15,f16,f5,f6&fid=f3&pn=1&pz=20&po=1&dect=1&wbp2u=|0|0|0|web';
    const data = await httpClient.get(url);
    return data.data.diff || [];
}

export async function fetchHKIndex() {
    const url =
        'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m%3A124%2Cm%3A125%2Cm%3A305&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf17%2Cf18%2Cf15%2Cf16%2Cf5%2Cf6&fid=f3&pn=1&pz=20&po=1&dect=1&wbp2u=%7C0%7C0%7C0%7Cweb';
    const data = await httpClient.get(url);
    return data.data.diff || [];
}

async function fetchETFDown(num: number = 40) {
    const url = `https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=b%3AMK0021%2Cb%3AMK0022%2Cb%3AMK0023%2Cb%3AMK0024%2Cb%3AMK0827&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf5%2Cf6%2Cf17%2Cf18%2Cf15%2Cf16&fid=f3&pn=1&pz=${num}&po=0&dect=1&wbp2u=%7C0%7C0%7C0%7Cweb`
    const data = await httpClient.get(url);
    return data.data.diff || [];
}

async function fetchGNDown(num: number = 40) {
    const url = `https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m%3A90%2Bt%3A3%2Bf%3A!50&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf20%2Cf8%2Cf104%2Cf105%2Cf128%2Cf140%2Cf141%2Cf207%2Cf208%2Cf209%2Cf136%2Cf222&fid=f3&pn=1&pz=${num}&po=0&dect=1&wbp2u=%7C0%7C0%7C0%7Cweb`
    const data = await httpClient.get(url);
    return data.data.diff || [];
}

async function fetchHYDown(num: number = 40) {
    const url = `https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m%3A90%2Bt%3A2%2Bf%3A!50&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf20%2Cf8%2Cf104%2Cf105%2Cf128%2Cf140%2Cf141%2Cf207%2Cf208%2Cf209%2Cf136%2Cf222&fid=f3&pn=1&pz=${num}&po=0&dect=1&wbp2u=%7C0%7C0%7C0%7Cweb`
    const data = await httpClient.get(url);
    return data.data.diff || [];
}

async function fetchEastMixDown() {
    const gnDown = await fetchGNDown();
    const hyDown = await fetchHYDown();
    const etfDown = await fetchETFDown(80);
    const downList = [...gnDown, ...hyDown, ...etfDown];
    return downList.sort((a, b) => a.f3 - b.f3) || [];
}

// db

export async function getEastBkzjMix() {
    return null

}